package cn.liangcaomei.educenter.service.impl;

import cn.liangcaomei.commonutils.JwtUtils;
import cn.liangcaomei.commonutils.MD5;
import cn.liangcaomei.educenter.entity.UcenterMember;
import cn.liangcaomei.educenter.entity.vo.RegisterVo;
import cn.liangcaomei.educenter.mapper.UcenterMemberMapper;
import cn.liangcaomei.educenter.service.UcenterMemberService;
import cn.liangcaomei.servicebase.exceptionhandler.CustException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author LiuJie
 * @since 2024-02-07
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(UcenterMember ucenterMember) {
        //获取手机号和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        //判断手机和密码是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new CustException(20001,"登录失败");
        }

        //判断手机号是否为空
        QueryWrapper mobWrapper = new QueryWrapper();
        mobWrapper.eq("mobile",mobile);
        UcenterMember mobMember = baseMapper.selectOne(mobWrapper);
        if (mobMember == null) {
            throw new CustException(20001,"手机号为空，登录失败");
        }

        //判断密码是否为空
        //把输入的密码加密后，再跟数据库的密码对比
        //加密方式 MD5
        if (!MD5.encrypt(password).equals(mobMember.getPassword())) {
            throw new CustException(20001,"密码为空，登录失败");
        }

        //判断用户是否为禁用
        if (mobMember.getIsDisabled()) {
            throw new CustException(20001,"用户已被禁用，登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具实现
        String jwtToken = JwtUtils.getJwtToken(mobMember.getId(), mobMember.getNickname());
        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //判断是否为空
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(password)) {
            throw new CustException(20001,"注册失败");
        }

        //判断验证码
        //先获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new CustException(20001,"验证校验失败");
        }

        //判断用户是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count  > 0) {
            throw new CustException(20001,"注册失败，用户已经存在");
        }

        //

        //将数据添加到数据库中
        //把 registerVo 对象的数据copy到 UcenterMember 对象中
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo,ucenterMember);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setAvatar("https://jack-edu.oss-cn-shenzhen.aliyuncs.com/2024/01/24/f14a144e7298465ca9d2d1144a60dfa5file.png");
        baseMapper.insert(ucenterMember);

    }
}
